<?php

/*
 * 赞的相关API
 */

/**********************
* 文件包含
*********************/
include_once('./data/tables.php');
include_once('./api/api_db.php');

/**********************
* 类定义
*********************/

/**********************
* 函数定义
*********************/
/**
 * 添加一条赞
 * @param type $suggest_weibo_id 要赞的公共墙微博id
 * @param type $user_id 微博用户id
 */
function like_add($suggest_weibo_id, $user_id) {
    global $glb_tb_like;
    global $glb_tb_suggest;
    
    if(!isset($suggest_weibo_id) || !isset($user_id)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        
        return $arr;
    }
    
    //判断是否存在，如果已存在，则返回错误；
    $where = COL_SUGGEST_ID . "= '" . $suggest_weibo_id . "' AND " . COL_USER_ID . "= '" . $user_id . "'";
    if(db_is_exist($glb_tb_like, $where)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_EXIST
        );
        
        return $arr;
    }
    
    $ret = false;
    //添加记录到赞表
    $struct = "(" . COL_SUGGEST_ID . ", " . COL_USER_ID . ")";
    $data = "('$suggest_weibo_id' , '$user_id')";
    if(db_run_insert($glb_tb_like, $struct, $data) != FALSE) {
        //修改公共墙微博表中赞的条数
        $set = COL_LIKE_NUM . " = " . COL_LIKE_NUM . " + 1";
        $where = COL_ID . "= " . $suggest_weibo_id;
        if(db_run_update($glb_tb_suggest, $set, $where) != FALSE) {
            $ret = true;
        } else {
            //...
        }
    }
    
    if($ret) {
        $arr = array(
            FLAG_RET => NO_ERR
        );
    } else {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_DB,
            FLAG_ERR_DETAIL => mysql_error()
        );
    }
    
    return $arr;
}

/**
 * 删掉一条赞
 * @param type $suggest_weibo_id 公共墙微博id
 * @param type $user_id 微博用户id
 */
function like_del($suggest_weibo_id, $user_id) {
    global $glb_tb_like;
    global $glb_tb_suggest;
    
    if(!isset($suggest_weibo_id) || !isset($user_id)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        
        return $arr;
    }
    
    //判断是否存在，如果不存在，则返回错误
    $where = COL_SUGGEST_ID . "= '" . $suggest_weibo_id . "' AND " . COL_USER_ID . "= '" . $user_id . "'";
    if(!db_is_exist($glb_tb_like, $where)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_NOT_EXIST
        );
        
        return $arr;
    }
    
    $ret = false;
    //删除赞表记录
    if(db_run_delete($glb_tb_like, $where) != FALSE) {
        //修改公共墙微博表中赞的条数
        $set = COL_LIKE_NUM . " = " . COL_LIKE_NUM . " - 1";
        $where = COL_ID . "= " . $suggest_weibo_id;
        if(db_run_update($glb_tb_suggest, $set, $where) != FALSE) {
            $ret = true;
        } else {
            //...
        }
    }
    
    if($ret) {
        $arr = array(
            FLAG_RET => NO_ERR
        );
    } else {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_DB,
            FLAG_ERR_DETAIL => mysql_error()
        );
    }
    
    return $arr;
}
?>
